package com.xjd.edu.common.admin.domain;


import com.xjd.edu.common.enums.AppType;
import com.xjd.edu.common.enums.OsEnum;
import com.xjd.edu.common.enums.PlatformEcoEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Arrays;
import java.util.stream.Collectors;

@Data
@Slf4j
public class ClientInfo implements Serializable {

    private static final long serialVersionUID = -6899506580381766705L;
    /**
     * _appName=khpw
     * &_appUser=913a7be89c43f8cb31f7a13b6d9da71e
     * &_cityName=武汉市
     * &_device=Simulator
     * &_firstTime=2018-07-31 09:51:27
     * &_gpsType=ip
     * &_imei=77777777777777777777777777777777
     * &_ipCity=420100
     * &_jail=false
     * &_latitude=0
     * &_launch=14
     * &_longitude=0
     * &_mac=77777777777777777777777777777777
     * &_manufacturer=Apple
     * &_network=wifi
     * &_pkgName=kaihei
     * &_platform=iphone
     * &_product=开黑陪玩
     * &_screenDip=3
     * &_screenHeight=2436
     * &_screenWidth=1125
     * &_system=iOS
     * &_systemVersion=11.4
     * &_userCity=110000
     * &_vendor=appstore
     * &_version=7.4.0
     * &_webviewVersion=4.7
     * &sign=a724cbc81eb21e191b8fd34549474ab2
     */

    private String _appName;
    private String _appUser;
    private String _cityName;
    private String _device;
    private String _deviceID_SM;
    private String _decrypt_deviceID_SM;
    private String _deviceID;
    private String _firstTime;
    private String _gpsType;
    private String _imei;
    private String _ipCity;
    private String _jail;
    private String _latitude;
    private String _launch;
    private String _longitude;
    private String _mac;
    private String _manufacturer;
    private String _network;
    private String _pkgName;
    private String _platform;
    private String _product;
    private String _screenDip;
    private String _screenHeight;
    private String _screenWidth;
    private String _system;
    private String _systemVersion;
    private String _userCity;
    private String _vendor;
    private String _version;
    private String _webviewVersion;
    private String _userAgent;
    private String _imei2;
    private String _oaid;
    private String _ip;
    private String sign;
    private AppType appType;
    private String h5UserAgent;
    private String h5DeviceIdSm;


    /**
     * 获取平台字段
     *
     * @return
     */
    public int getPlatform() {
        if (StringUtils.isNumeric(get_platform())) {
            return Integer.parseInt(get_platform());
        }
        //判断如果是android设备并且platform为空则返回android通用类型
        if (OsEnum.ANDROID.getType().equals(OsEnum.parseByClientInfo(get_system()))) {
            return PlatformEcoEnum.ANDROID.getType();
        }
        return PlatformEcoEnum.UNKNOW.getType();
    }

    /**
     * 获取平台字段
     *
     * @return
     */
    public int getAppTypeByUa() {
        if (StringUtils.isEmpty(h5UserAgent)) {
            return AppType.PI_PI.getValue();
        }
        return h5UserAgent.contains(AppType.PLAYMATE.getUa()) ? AppType.PLAYMATE.getValue() : AppType.PI_PI.getValue();
    }

    /**
     * 判断是否是使用ios设备
     *
     * @return
     */
    public boolean isIosDevices() {
        return StringUtils.endsWithIgnoreCase("iOS", get_system());
    }


    public String get_deviceID_SM() {
        /*if (_decrypt_deviceID_SM != null) {
            return _decrypt_deviceID_SM;
        }
        if (StringUtils.isEmpty(_deviceID_SM)) {
            return _deviceID_SM;
        }
        String decode = URLDecoder.decode(_deviceID_SM);
        if (BoxIdTranslator.isBoxId(decode)) {
            String deviceNo = BoxIdTranslator.outputSmid(decode, Constant.SHUMEI_PRIVATE_KEY);
            log.info("解密后的指纹:{}", splitString(deviceNo));
            _decrypt_deviceID_SM = deviceNo;
            return _decrypt_deviceID_SM;
        }*/
        return _deviceID_SM;
    }

    private String splitString(String s) {
        if (StringUtils.isBlank(s)) {
            return s;
        }
        return Arrays.asList(s.split("")).stream().collect(Collectors.joining("_"));
    }
}
